<template>
    <!-- 导航栏 -->
    <van-nav-bar title="新增收货地址" left-text="返回" left-arrow @click-left="back" />

    <!-- 新增地址 -->
    <van-address-edit
        :area-list="areaList"
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :address-info="AddressInfo"
        @save="onSave"
    />
</template>
<script>
    import { areaList } from '@vant/area-data';
    export default {
        emits: ['toggle'],
        created() {
            this.$emit('toggle', false);
            this.LoginUser = this.$cookies.get('LoginUser')
            this.action = this.$route.query.action ? this.$route.query.action : ''
            this.cartids = this.$route.query.cartids ? this.$route.query.cartids : '';
        },
        data(){
            return{
                areaList,
                AddressInfo: {},
                LoginUser: {},
                action:null,
                cartids: null
            }
        },
        methods: {
            back(){
                this.$router.push('/user/address/index');
            },
            async onSave(content){

                // 组装数据
                var data = {
                    consignee: content.name,
                    mobile: content.tel,
                    code: content.areaCode,
                    address: content.addressDetail,
                    status: content.isDefault ? 1 : 0,
                    userid: this.LoginUser.id
                }
                let result = await this.$api.AddressAdd(data)
                if(result.code == '1'){
                    this.$notify({
                        type: 'success',
                        message: result.msg,
                        duration: 1000,
                        onClose: () => {
                            if(this.action == 'order'){
                                this.$router.replace({
                                    path: '/product/cart/confirm',
                                    query: {
                                        cartids: this.cartids
                                    }
                                })
                            } else{
                                this.back()
                            }
                        }
                    })
                } else{
                    this.$notify({
                        type: 'danger',
                        message: result.msg,
                        duration: 1000
                    })
                }
            }
        }
    }
</script>